﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FinanceTool.Entities;
using FinanceTool.Data;
using FinanceTool.Components.Parsers;

namespace FinanceTool.Components
{
    internal class IngSourceFileReader : SourceFileReader
    {
        private string _fileLocation;

        public override bool HasFirstLine
        {
            get { return true; }
        }

        public IngSourceFileReader(string fileLocation)
        {
            if (string.IsNullOrEmpty(fileLocation)) throw new ArgumentException("FileLocation cannot be null or empty.", "fileLocation");
            _fileLocation = fileLocation;
        }

        public override List<Transaction> ReadTransactions()
        {
            var retVal = new List<Transaction>();

            // Read ING source file
            bool firstLine = true;
            if (TransactionList.Count == 0)
            {
                using (var reader = new IngCvsReader(_fileLocation))
                {
                    string strline = "";
                    string[] values = null;
                    int x = 0;
                    while (!reader.EndOfStream)
                    {
                        x++;
                        strline = reader.ReadLine();
                        values = strline.Split(Convert.ToChar(reader.Delimiter));
                        if (!firstLine)
                        {
                            // Parse to Transaction
                            Transaction transaction = IngParser.Instance.ParseToTransaction(values);
                            if (transaction != null) TransactionList.Add(transaction);
                        }
                        firstLine = false;
                    }
                }              
            }
            
            // Copy to local result
            retVal = TransactionList;                        

            return retVal;
        }
    }
}
